package org.deng.learn.netty.client;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.IOException;
/**
 * @author 邓力宾
 * @Desc
 * @since 2020/6/15 下午2:02
 */
public class Client {
    public static String host = "127.0.0.1";  //ip地址
    public static int port = 8000;          //端口
    /// 通过nio方式来接收连接和处理连接

    public static void start() throws IOException, InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        try{
            Bootstrap b = new Bootstrap();
            b.group(group);
            b.channel(NioSocketChannel.class);
            b.handler(new MyClientChannelInitializer());
            // 连接服务端
            // 发起异步连接操作
            ChannelFuture f = b.connect(host, port).sync();

            // 等待客户端链路关闭
            f.channel().closeFuture().sync();
        }finally {
            // 优雅退出，释放线程池资源
            group.shutdownGracefully();
        }



    }
    /**
     * Netty创建全部都是实现自AbstractBootstrap。
     * 客户端的是Bootstrap，服务端的则是    ServerBootstrap。
     **/
    public static void main(String[] args) throws InterruptedException, IOException {

        start();
    }

}
